//
//  StatusOriginalView.swift
//  SinaWeibo
//
//  Created by yang on 15/11/14.
//  Copyright © 2015年 yang. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
import FFLabel
class StatusOriginalView: UIView ,FFLabelDelegate{
    var bottomConstraints:Constraint?
    var status :Status?{
        didSet{
           iconImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            memberIconView.image = status?.user?.mbrankImage
            verifiedIconView.image = status?.user?.verified_typeImage
            timeLabel.text =  NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
            sourceLabel.text = status?.weiboSource?.linhref().clineName
            contentLabel.attributedText = EmoticonManager.emoticonManager.textToImageText(status?.text ?? "", font: contentLabel.font)
            //移除约束
            self.bottomConstraints?.uninstall()
            if let picture = status?.urlImages where picture.count > 0{
                pictureView.imageURLs = status?.urlImages
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView).offset(StatusCellMargin).constraint
                })
            }else{
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(contentLabel).offset(StatusCellMargin).constraint
                })
                
            }

        }
    }
    override init(frame: CGRect) {
        super.init(frame:frame)
        contentLabel.labelDelegate = self;  
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        sepView.backgroundColor = UIColor(white: 0.8, alpha: 1)
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(verifiedIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(20)
        }
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(sepView).offset(StatusCellMargin)
            make.height.width.equalTo(StatusCellIconWitdth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel)
        }
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(iconImage.snp_bottom)
            make.centerX.equalTo(iconImage.snp_right)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconImage)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImage)
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel)
        }
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraints = make.bottom.equalTo(pictureView).offset(StatusCellMargin).constraint
        }
        
    }
    private lazy var sepView :UIView = UIView()
    //头像
    private lazy var iconImage :UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //用户名
    private lazy var nameLabel:UILabel = UILabel(title:"淡淡的月光", size: 14, color: UIColor.darkGrayColor())
    //用户等级
    private lazy var memberIconView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //用户认证类型
    private lazy var verifiedIconView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: 10, color: UIColor.orangeColor())
    //微博来源
    private lazy var sourceLabel:UILabel = UILabel(title: "来自: 刘涛", size: 10, color: UIColor.lightGrayColor())
    //微博正文
    private lazy var contentLabel:FFLabel = FFLabel(title: "野内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮", size: 14, color: UIColor.lightGrayColor(),margin: StatusCellMargin)
    //图片
    private lazy var pictureView:StatusPictureView = StatusPictureView()

}
extension StatusOriginalView{
    func labelDidSelectedLinkText(label: FFLabel, text: String){
        if text.hasPrefix("http") {
            let web = HomeWebViewController()
            web.urlString = text
            self.NavController()?.pushViewController(web, animated: true)
        }
    }

}
